package yxy.game.pm2.command.soul;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.hero.SoulHandle;

import java.util.Map;

/** 魂玉 所有魂玉 */
@MessageType(proto = Command.Souls, classtype = HeroMessage.SoulsRequest.class)
public class CMD_Souls extends Command<HeroMessage.SoulsRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.SoulsRequest request) {
		String playerCode = messageSource.getTag();

		HeroMessage.SoulsResponse.Builder response = HeroMessage.SoulsResponse.newBuilder();

		Map<String, Soul> souls = Soul.all(playerCode);
		for (Soul soul : souls.values()) {
			if (soul.getHero() == null) {
				// 无人穿戴的魂玉CMD_Souls
				response.addSouls(new SoulHandle().packSoul(soul));
			}
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, HeroMessage.SoulsResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}